1
|
|
|
import { execSync } from "child_process" |
2
|
|
|
import { appendFileSync, readFileSync, writeFileSync } from "fs" |
3
|
|
|
import { |
4
|
|
|
eol, |
5
|
|
|
outputDir |
6
|
|
|
} from "../../config" |
7
|
|
|
import { join } from "../utils" |
8
|
|
|
|
9
|
|
|
type AssocCell = string|undefined|null |
10
|
|
|
type AssocRecord = Record<string, AssocCell> |
11
|
|
|
type AssocTable = Record<string, AssocRecord> |
12
|
|
|
|
13
|
|
|
if (!module.parent) |
14
|
|
|
main() |
15
|
|
|
|
16
|
|
|
export { } |
17
|
|
|
|
18
|
|
|
function main() { |
19
|
|
|
const collected = collect() |
20
|
|
|
, table = json2Table(collected) |
21
|
|
|
, md2 = array2md(table) |
22
|
|
|
, md1 = array2md(transpose(table)) |
23
|
|
|
|
24
|
|
|
writeFileSync(`${outputDir}.json`, JSON.stringify(collected, null, 2)) |
25
|
|
|
appendFileSync(`${outputDir}.md`, md1) |
26
|
|
|
appendFileSync(`${outputDir}.md`, "\n\n") |
27
|
|
|
appendFileSync(`${outputDir}.md`, md2) |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function collect() { |
31
|
|
|
const map: AssocTable = {} |
32
|
|
|
, files = lining(execSync(`find ${outputDir} -type f`)) |
33
|
|
|
// TODO .concat("input.env") |
34
|
|
|
.sort() |
35
|
|
|
|
36
|
|
|
for (let i = 0; i < files.length; i++) { |
37
|
|
|
const filePath = files[i] |
38
|
|
|
, content = lining(readFileSync(filePath)) |
39
|
|
|
.filter(x => x.match(/^[a-z]/i)) |
40
|
|
|
, fileMap = map[filePath.replace(/^(.*)\//, "")] = {} as AssocRecord |
41
|
|
|
|
42
|
|
|
for (let l = 0; l < content.length; l++) { |
43
|
|
|
const [,variable, value = null] = content[l].match(/([^=]+)=?(.*)/) ?? [] |
44
|
|
|
|
45
|
|
|
fileMap[`${variable}=`] = value |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return map |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function transpose<Cell>(source: Cell[][]) { |
53
|
|
|
const $return: Cell[][] = [] |
54
|
|
|
|
55
|
|
|
for (let i = 0; i < source.length; i++) { |
56
|
|
|
const sourceRow = source[i] |
57
|
|
|
for (let j = 0; j < sourceRow.length; j++) { |
58
|
|
|
const next = $return[j] = $return[j] ?? [] |
59
|
|
|
next[i] = sourceRow[j] |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $return |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function json2Table(source: AssocTable) { |
67
|
|
|
const headers: string[] = [] |
68
|
|
|
, interTable: Record<string, AssocCell[]> = {} |
69
|
|
|
|
70
|
|
|
for (const key in source) { |
71
|
|
|
const index = headers.push(`**${key}**`) |
72
|
|
|
, record = source[key] |
73
|
|
|
|
74
|
|
|
for (const prop in interTable) |
75
|
|
|
(interTable[prop] = interTable[prop] ?? []) |
76
|
|
|
.push(record[prop]) |
77
|
|
|
|
78
|
|
|
for (const prop in record) { |
79
|
|
|
if (prop in interTable) |
80
|
|
|
continue |
81
|
|
|
(interTable[prop] = new Array(index - 1).fill(undefined)) |
82
|
|
|
.push(record[prop]) |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
const table = [([undefined] as AssocCell[]).concat(headers)] |
87
|
|
|
for (const prop in interTable) |
88
|
|
|
table.push(([`**${prop}**`] as AssocCell[]).concat(interTable[prop])) |
89
|
|
|
|
90
|
|
|
return table |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function array2md(array: AssocCell[][]) { |
94
|
|
|
const table: string[] = [] |
95
|
|
|
|
96
|
|
|
for (let i = 0; i < array.length; i++) { |
97
|
|
|
table.push(row2md(array[i])) |
98
|
|
|
|
99
|
|
|
if (i === 0) |
100
|
|
|
table.push(row2md(new Array(array[0].length).fill("-"))) |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return join(table) |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function row2md(arr: AssocCell[]) { |
107
|
|
|
return `| ${ |
108
|
|
|
arr |
109
|
|
|
.map(x => x?.replace?.(/([`])/g, "\\$1") ?? x) |
110
|
|
|
.map(x => |
111
|
|
|
x === undefined ? "-" |
112
|
|
|
: x === null ? "null" |
113
|
|
|
: x === "" ? "" |
114
|
|
|
: x |
115
|
|
|
) |
116
|
|
|
.join(" | ") |
117
|
|
|
} |` |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function lining(source: Pick<Object, "toString">) { |
121
|
|
|
return source |
122
|
|
|
.toString() |
123
|
|
|
.split(eol) |
124
|
|
|
.filter(x => x) |
125
|
|
|
} |
126
|
|
|
|